﻿using System;
using System.Web.UI;
using SARS.Biz;
using SARS.Entity;

namespace SARS.Tutor
{
    public partial class ReviewAbsentRequest : Page
    {
        private readonly StudentAbsentReasonBO absentBO = new StudentAbsentReasonBO();
        private readonly CourseBO courseBiz = new CourseBO();
        private readonly ProgramBO programBiz = new ProgramBO();
        private readonly SessionBO sessionBiz = new SessionBO();
        private readonly UserBO userBiz = new UserBO();
        private StudentAbsenceReasonEntity absenceReason;
        private string attendenceId = string.Empty;
        private CourseEntity course;
        private ProgramEntity program;
        private SessionEntity session;
        private StudentEntity student;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();
            //TODO: Update dont using this query string by dropdownlist
            string sessionId = Request.QueryString["sesid"];
            string courseId = Request.QueryString["coid"];
            string studentId = Request.QueryString["stuid"];
            string programId = Request.QueryString["proid"];
            attendenceId = Request.QueryString["atsesid"];


            if (!string.IsNullOrEmpty(sessionId))
            {
                session = sessionBiz.GetSessionById(Int32.Parse(sessionId));
                course = courseBiz.RetrieveCourseById(Int32.Parse(courseId));
                program = programBiz.RetrieveProgramById(Int32.Parse(programId));
                student = (StudentEntity) userBiz.RetrieveUserById(Int32.Parse(studentId));
                absenceReason = absentBO.GetStudentAbsenceReasonById(Int32.Parse(attendenceId));
                if (!IsPostBack)
                {
                    if (absenceReason.AttendanceSessionId == Int32.Parse(attendenceId))
                    {
                        txtReason.Text = absenceReason.StudentAbsenceReason;
                        UserEntity approver = userBiz.RetrieveUserById(absenceReason.ModifiedBy);
                        if (approver != null)
                        {
                            lblApprover.Text = approver.FullName;
                        }

                        DropDownList1.SelectedValue = absenceReason.IsApproved.ToString();
                    }

                    lblProgram.Text = program.ProgramName;
                    lblCourse.Text = course.CourseInfo;
                    lblSession.Text = session.SessionTopic;
                    lblRoom.Text = session.Room.RoomName;
                    lblTutor.Text = session.Manager.FullName;
                    lblStudent.Text = student.FullName;
                    lblStart.Text = session.StartDate.ToString();
                }
            }
        }

        protected void btnCancel_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("StudentAbsentReasons.aspx");
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "0")
            {
                Response.Write("<script>alert('Please select status for this absent request.')</script");
                return;
            }
            UserEntity user = userBiz.RetrieveUserByUserName(Page.User.Identity.Name);
            if (absenceReason != null)
            {
                absenceReason.IsApproved = Int32.Parse(DropDownList1.SelectedValue);
                absenceReason.ModifiedBy = user.UserID;

                int result = absentBO.UpdateStudentAbsenceReason(absenceReason);
                if (result > 0)
                {
                    Response.Write("<script>alert('Update successfull.')</script");
                    //string linkurl = string.Format("ReviewAbsentRequest.aspx?atsesid={0}&sesid={1}&coid={2}&proid={3}&stuid={4}"
                    //                           , attendenceId
                    //                           , session.SessionID
                    //                           , course.CourseID
                    //                           , program.ProgramID
                    //                           , student.UserID);
                    //Response.Redirect(linkurl);
                }
            }
        }
    }
}